package com.sushi.service.impl;

import com.sushi.repository.entity.FileEntity;
import com.sushi.repository.mapper.FileMapper;
import com.sushi.service.FileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sushi.response.ResponseBO;
import com.sushi.response.ResponsePageBO;
import com.sushi.response.FileResp;
import com.sushi.request.FileReq;
import com.sushi.request.FilePageReq;
import com.sushi.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sushi.exception.BusinessException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件 服务实现类
 * @date 2023-05-02
 * @time 17:14:13
 * @author Administrator
 */
@Slf4j
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, FileEntity> implements FileService {

	@Autowired
	private FileMapper fileMapper;

    

    /**
     * 新增
     */
    @Override
    public ResponseBO<FileResp> add(FileReq req) {
        FileEntity entity = BeanUtil.convert(req, FileEntity.class);
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("保存失败");
        }
        entity.insert();
        return ResponseUtil.ok(BeanUtil.convert(entity, FileResp.class));
    }

    /**
     * 逻辑删除
     */
    @Override
    public ResponseBO<FileResp> logicRemove(FileReq req) {
        if (StringUtil.isBlank(req.getId())) {
            throw new BusinessException("id不能为空");
        }
        FileEntity entity = new FileEntity();
        entity.setId(req.getId());
        entity.setDeleted(1L);
        entity.updateById();
        return ResponseUtil.ok(BeanUtil.convert(entity, FileResp.class));
    }

    /**
     * 更新
     */
    @Override
    public ResponseBO<FileResp> updateByField(FileReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        FileEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            return ResponseUtil.error("信息不存在，无法更新");
        }
        if (StringUtil.isNotBlank(req.getTitle())) {
            entity.setTitle(req.getTitle());
        }
        if (StringUtil.isNotBlank(req.getUrl())) {
            entity.setUrl(req.getUrl());
        }
        if (ObjectUtil.isNotEmpty(req.getSize())) {
            entity.setSize(req.getSize());
        }

        entity.updateById();
        log.info("更新成功：{}", JSON.toJSONString(entity));
        return ResponseUtil.ok(BeanUtil.convert(entity, FileResp.class));
    }

    /**
     * 列表
     */
    @Override
    public ResponsePageBO<FileResp> listByCondition(FilePageReq req) {
        if (ObjectUtil.isEmpty(req) || ObjectUtil.isEmpty(req.getPageNum()) || ObjectUtil.isEmpty(req.getPageSize())) {
            throw new BusinessException("必要参数不能为空");
        }
        LambdaQueryWrapper<FileEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FileEntity::getDeleted, 0L);
        wrapper.eq(StringUtil.isNotBlank(req.getId()), FileEntity::getId, req.getId());
        wrapper.like(StringUtil.isNotBlank(req.getTitle()), FileEntity::getTitle, req.getTitle());
        wrapper.like(StringUtil.isNotBlank(req.getUrl()), FileEntity::getUrl, req.getUrl());
        wrapper.eq(ObjectUtil.isNotEmpty(req.getSize()), FileEntity::getSize, req.getSize());

        Page<FileEntity> page = page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        if (ObjectUtil.isEmpty(page) || CollectionUtil.isEmpty(page.getRecords())) {
            return ResponsePageUtil.ok(req, new ArrayList<>(), 0L);
        }
        List<FileResp> convert = BeanUtil.convert(page.getRecords(), FileResp.class);
        return ResponsePageUtil.ok(req, convert, page.getTotal());
    }

    /**
     * 详情
     */
    @Override
    public ResponseBO<FileResp> detail(FileReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        FileEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("对象不存在");
        }
        return ResponseUtil.ok(BeanUtil.convert(entity, FileResp.class));
    }


    @Override
    public List<FileResp> listByIdList(List<String> list) {

        List<FileEntity> entityList = list(new LambdaQueryWrapper<FileEntity>().eq(FileEntity::getDeleted, 0L).in(FileEntity::getId, list));
        List<FileResp> convert = BeanUtil.convert(entityList, FileResp.class);
        return CollectionUtil.isEmpty(convert) ? new ArrayList<>() : convert;
    }

}